"use strict";
cc._RFpush(module, '76e7cHKU55CNonaiPA0fNIB', 'Cube');
// scripts\Cube.js

require('Contact');
var Game = require('Game');
cc.Class({
    'extends': cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...

    },

    // use this for initialization
    onLoad: function onLoad() {
        var self = this;
        this.firstContract = false;
        this.speed = Game.instance.speed;
        this.alive = true;
        // 旋转当中计算真实的可视高度
        this.viewHigh = this.node.height;
        // 物理组件相关
        this.physicsCubeBody = this.getComponent(Body);
        this.physicsCubeBody.bodyType = 2;
        this.downWallHeight = Game.instance.wallDownNode.height / 2;
        //给一个初始的降落力，力度大小应配成参数
        this.physicsCubeBody.body.ApplyForce(new b2Vec2(0, -300 * this.physicsCubeBody.body.GetMass()), this.physicsCubeBody.body.GetWorldCenter());

        this.physicsCubeBody.addContactEvent(ContactType.BEGIN_CONTACT, function () {
            //var self = this;
            if (!self.firstContract) {

                self.physicsCubeBody.bodyType = 2;
                self.firstContract = true;

                var currentMeter = self.getHighMeter();
                //console.log(self.node.y + " : " + currentMeter);
                if (currentMeter > Game.instance.highMeter) {
                    Game.instance.updateHighMeter(currentMeter);
                }
                // 如果当前游戏最高点显示在大于屏幕的1/3位置，屏幕（gameView）下移
                if (currentMeter + Game.instance.gameArea.y > Game.instance.gameArea.height / 3) {
                    Game.instance.moveBg(currentMeter + Game.instance.gameArea.y - Game.instance.gameArea.height / 3);
                }
                //Game.instance.generateCube();
            }
        });

        //this.angle = Math.atan(this.node.height/this.node.width);
        //this.diagonal = this.node.height/Math.sin(this.angle);
        //console.log(this.diagonal);
        //this.node.runAction(cc.follow(Game.instance.gameArea));
    },
    // called every frame, uncomment this function to activate update callback
    update: function update(dt) {

        //console.log(this.node.y);
        //console.log(this.node.rotationX+"::"+this.node.rotationY);
        if (!this.firstContract) {
            //落地后变成动态物理组件
            /*if(this.node.y - 2*this.speed  <= this.downWallHeight - Game.instance.gameArea.height/2 + this.viewHigh/2){
                //留出0.1的距离自由落体，实现落地并且触发BEGIN_CONTACT
                this.node.y = this.downWallHeight + 0.1 - Game.instance.gameArea.height/2 + this.viewHigh/2;
                this.physicsCubeBody.bodyType = 2;
            }
            else{
                this.node.y -= 2 * this.speed;
            }*/
            //碰撞之前持续给出反重力使节点匀速运动
            this.physicsCubeBody.body.ApplyForce(new b2Vec2(0, 9.8 * this.physicsCubeBody.body.GetMass()), this.physicsCubeBody.body.GetWorldCenter());
        }

        // 误差预留量，不可能精确到1像素，因为节点会倾斜，此时最低点不能按照重心计算
        /*if(this.node.y < this.downWallHeight - Game.instance.gameArea.height/2){
            if(this.alive){
                Game.instance.subLife(1);
                this.alive = false;
            }
        }*/
        // 出左右边界后销毁并触发新节点
        if (this.node.getBoundingBox().xMax < Game.instance.leftBorder || this.node.getBoundingBox().xMin > Game.instance.rightBorder) {
            if (this.node.uuid === Game.instance.physicsControlCube.uuid) {
                Game.instance.generateCube();
            }
            this.node.destroy();
            Game.instance.subLife(1);
            //console.log("destroyed");
        }
        //出下边界并不销毁，只是触发新节点
        if (this.node.getBoundingBox().yMax + Game.instance.gameArea.y < Game.instance.bottomBorder) {
            if (this.node.uuid === Game.instance.physicsControlCube.uuid) {
                Game.instance.generateCube();
                Game.instance.subLife(1);
            }
        }
    },
    onLeft: function onLeft(delta) {
        if (!this.firstContract) {
            this.node.x -= delta;
            // 判断撞墙，兼容旋转后的情况
            if (this.node.getBoundingBox().xMin < 0 - Game.instance.gameArea.width / 2) {
                this.node.x += delta;
            }
            //console.log(this.node.x+":"+this.node.getBoundingBox().xMin);
        }
    },
    onRight: function onRight(delta) {
        if (!this.firstContract) {
            this.node.x += delta;
            if (this.node.getBoundingBox().xMax > Game.instance.gameArea.width / 2) {
                this.node.x -= delta;
            }
            //console.log(this.node.x);
        }
    },
    onChangeDirection: function onChangeDirection() {
        if (!this.firstContract) {
            //console.log(this.node.getBoundingBox().xMax+":"+this.node.getBoundingBox().xMin+":"+this.node.getBoundingBox().yMax+":"+this.node.getBoundingBox().yMin);
            var action = cc.rotateBy(0, 90);
            this.node.runAction(action);

            // 调整高和宽
            if (this.viewHigh === this.node.height) {
                this.viewHigh = this.node.width;
            } else {
                this.viewHigh = this.node.height;
            }
        }
    },
    onRapidDownStart: function onRapidDownStart() {

        this.speed = 10;
    },
    onRapidDownEnd: function onRapidDownEnd() {
        this.speed = 1;
    },
    /**
     * 根据参数计算cube的最高点的值
     * gameViewHeight：游戏区域高度（正数）
     * cubeHighY：节点Y值，如果在半屏以下，为负数
     * platFormY2：底部碰撞平台的高度的一半
     * gameViewY：游戏背景区域移动的距离,负数
     */
    getHighMeterWithParam: function getHighMeterWithParam(gameViewHeight, cubeHighY, platFormY2, gameViewY) {
        //return gameViewHeight+cubeHighY-platFormY2-gameViewY;
        return Math.floor(gameViewHeight / 2 + cubeHighY - platFormY2);
    },
    getHighMeter: function getHighMeter() {
        return this.getHighMeterWithParam(Game.instance.gameArea.height, this.node.getBoundingBox().yMax, this.downWallHeight);
    }
});

cc._RFpop();